import cv2
import numpy as np
from utils.common import load_image, show_image, make_dirs  # 导入公共函数
from utils.config import exp4_adaptive_path, exp4_output_dir  # 导入输出路径

# 确保输出目录存在
make_dirs(exp4_output_dir)

# 加载图片
image = load_image()

# 将图像转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 自适应阈值化
adaptive_binary_img = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

# 显示结果
cv2.imshow("Adaptive Binary Image", adaptive_binary_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 保存结果
cv2.imwrite(exp4_adaptive_path, adaptive_binary_img)
print(f"自适应阈值化已保存：{exp4_adaptive_path}")
